package com.example.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.Student;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.IStudentService;
import com.example.demo.service.IUserService;
import com.example.demo.utils.AjaxJson;
import com.example.demo.vo.studentAddVo;
import com.example.demo.vo.studentSelectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 学生信息表 前端控制器
 * </p>
 *
 * @author
 * @since 2025-11-10
 */
@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private IStudentService studentService;

    @Autowired
    IUserService userService;

    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/add")
    public AjaxJson add(@RequestBody studentAddVo student) {
        AjaxJson ajaxJson = userService.addRegisterByStudent(student);
        if(ajaxJson.getCode()!=200){
            return ajaxJson;
        }else {
            User data = ajaxJson.getData(User.class);
            Student student1 = new Student();
            student1.setUserId(data.getUserId());
            student1.setClassId(student.getClassId());
            student1.setStudentNo(student.getStudentNo());
            student1.setEnrollmentDate(student.getEnrollmentDate());
            student1.setCreateTime(LocalDateTime.now());
            boolean save = this.studentService.update(student1, new QueryWrapper<Student>().eq("user_id", data.getUserId()));
            if (save) {
                return AjaxJson.getSuccess("添加成功");
            } else {
                return AjaxJson.getError("添加失败");
            }
        }
    }

    @RequestMapping("/delete")
    public AjaxJson delete(@RequestParam Integer id) {
        System.out.println(id);
        Student user = studentService.getById(id);
        System.out.println(user);
        Integer userId1 = user.getUserId();
        System.out.println("userId1");
        int userId = userMapper.delete(new QueryWrapper<User>().eq("user_id", userId1));
        if (userId == 1) {
            return studentService.removeById(id) ? AjaxJson.getSuccess("删除成功") : AjaxJson.getError("删除失败");

        } else {
            return AjaxJson.getError("删除失败");
        }
    }

    @RequestMapping("/update")
    public AjaxJson update(@RequestBody Student student) {
        student.setUpdateTime(LocalDateTime.now());
        return studentService.updateById(student) ? AjaxJson.getSuccess("修改成功") : AjaxJson.getError("修改失败");
    }

    @RequestMapping("/query")
    public AjaxJson query() {
        List<studentSelectVo> vos = studentService.queryList();
        return AjaxJson.getPageData((long) vos.size(), vos);
    }

    @RequestMapping("/query/id")
    public AjaxJson query(@RequestParam Integer id) {
        studentSelectVo student = studentService.queryById(id);
        return AjaxJson.getSuccessData(studentService.getById(id));
    }
}